<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
      class="query-form"
    >
      <el-form-item label="预案名称" prop="planTitle">
        <el-input
          v-model="queryParams.planTitle"
          placeholder="请输入预案名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预警级别" prop="alarmLevel">
        <el-select v-model="queryParams.alarmLevel" placeholder="请输入预警级别" clearable size="small">
          <el-option v-for="dict in alarmLevel" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
          <el-option label="未发布" value="0" />
          <el-option label="已发布" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="预警时间" prop="alarmTime">
        <el-date-picker
          v-model="queryParams.alarmTime"
          clearable
          size="small"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间"
        />
        <span style="padding: 0 4px;">-</span>
        <el-date-picker
          v-model="queryParams.endTime"
          clearable
          size="small"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
        <el-button size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="content-container">
      <div class="handle_bar">
        <div class="tit"><span>应用列表</span></div>
        <div>
          <el-button
            type="primary"
            plain
            size="small"
            @click="handleAdd"
          >新增</el-button>
        </div>
      </div>
      <div class="flex-1" style="position: relative;overflow: hidden;">
        <div style="position: absolute;left: 0;top: 0;bottom: 0;right: 0;display: flex;flex-direction: column;overflow: hidden;">
          <el-table
            v-loading="loading"
            :data="releaseList"
            class="flex-1"
            height="250"
            style="width:100%;"
            @selection-change="handleSelectionChange"
          >
            <!-- <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="ID" align="center" prop="id" /> -->
            <!-- :formatter="alarmLevelFormat" -->
            <!-- :formatter="planTypeFormat"  -->
            <el-table-column label="预警级别" align="center" prop="alarmLevel" />
            <el-table-column label="预案名称" align="center" prop="planTitle" />
            <el-table-column label="预案类型" align="center" prop="planType" />
            <el-table-column label="预案内容" align="center" prop="planAbstract" />
            <el-table-column label="预警时间" align="center" prop="alarmTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.alarmTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="createUserName" />
            <el-table-column label="处理状态" align="center" prop="status">
              <template slot-scope="scope">
                <span v-if="scope.row.status == 0">未发布</span>
                <span v-if="scope.row.status == 1">已发布</span>
              </template>
            </el-table-column>
            <el-table-column label="通知方式" align="center" prop="noticeType">
              <template slot-scope="scope">
                <span v-if="scope.row.noticeType == 2">电话</span>
                <span v-if="scope.row.noticeType == 1">短信</span>
              </template>
            </el-table-column>
            <!-- <el-table-column label="创建人" align="center" prop="createUserName" /> -->
            <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <template v-if="scope.row.status == 0">
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleRelease(scope.row)"
                  >发布</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleUpdate(scope.row)"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleDelete(scope.row)"
                  >删除</el-button>
                </template>
                <template v-if="scope.row.status == 1">
                  <el-button
                    size="mini"
                    type="text"
                    @click="handleView(scope.row)"
                  >查看</el-button>
                  <!-- <el-button
                    size="mini"
                    type="text"
                  >响应情况</el-button> -->
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- 添加或修改预警发布管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="696px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top" size="small">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="预案名称" prop="confAlarmId">
              <el-select
                v-model="form.confAlarmId"
                placeholder="请选择预案名称"
                style="width: 100%;"
                @change="confAlarmChange"
              >
                <el-option
                  v-for="item in plansNameAll"
                  :key="item.id"
                  :label="item.planTitle"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预案类型" prop="planType">
              <el-select v-model="form.planType" placeholder="请选择预案类型" style="width: 100%;" disabled>
                <el-option v-for="dict in planTypes" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警级别" prop="alarmLevel">
              <el-select v-model="form.alarmLevel" placeholder="请选择预警级别" style="width: 100%;" disabled>
                <el-option v-for="dict in alarmLevel" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警时间" prop="alarmTime">
              <el-date-picker
                v-model="form.alarmTime"
                clearable
                size="small"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择预警时间"
                style="width:100%;"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="预案内容" prop="planAbstract">
              <el-input v-model="form.planAbstract" type="textarea" placeholder="请输入预案内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入预案内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="通知人员" prop="noticeUserList">
              <el-tag v-for="tag in form.noticeUserList" :key="tag.id" closable :disable-transitions="false" @close="handleClose(tag)">{{ tag.directoryName }}——{{ tag.personName }}</el-tag>
              <el-button class="button-new-tag" type="primary" size="small" @click="selectUser">+ 人员</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="通知方式" prop="noticeType">
              <el-radio-group v-model="form.noticeType">
                <el-radio label="1">短信</el-radio>
                <el-radio label="2">电话</el-radio>
                <!-- <el-checkbox label="站内信" name="2" />
                <el-checkbox label="电话" name="3" /> -->
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!--  <el-form-item label="创建人" prop="createUser">
            <el-input v-model="form.createUser" placeholder="请输入创建人" />
          </el-form-item> -->
          <!-- <el-form-item label="处理状态">
            <el-radio-group v-model="form.status">
              <el-radio label="1">请选择字典生成</el-radio>
            </el-radio-group>
          </el-form-item> -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="formDisabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <SelectUser ref="SelectUser" @success="onSelectUser" />
  </div>
</template>

<script>
import { listRelease, getRelease, delRelease, addRelease, updateRelease } from '@/api/fjgs/warning/plans/publish.js'
import { getAll } from '@/api/fjgs/warning/plans/config.js'
import SelectUser from './components/SelectUser.vue'
export default {
  name: 'Release',
  components: {
    SelectUser
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警发布管理表格数据
      releaseList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alarmLevel: null,
        planType: null,
        planTitle: null,
        planAbstract: null,
        alarmTime: null,
        noticeType: null,
        createUser: null,
        status: null
      },
      // 表单参数
      form: {
        noticeUserList: []
      },
      // noticeUserList: [],
      // 表单校验
      rules: {
        alarmLevel: [{ required: true }],
        planType: [{ required: true }],
        planTitle: [{ required: true }],
        planAbstract: [{ required: true }],
        alarmTime: [{ required: true }],
        noticeType: [{ required: true }]
      },
      planTypes: [],
      alarmLevel: [],
      plansNameAll: []
    }
  },
  created() {
    this.getList()
    this.getDicts('plan_type').then(response => {
      this.planTypes = response.data
    })
    this.getDicts('alarm_level').then(response => {
      this.alarmLevel = response.data
    })
    getAll().then(res => {
      this.plansNameAll = res.data
    })
  },
  methods: {
    selectUser() {
      this.$refs.SelectUser.open()
    },
    onSelectUser(users) {
      users.forEach(u => {
        const has = this.form.noticeUserList.find(e => e.personId === u.id)
        if (!has) {
          this.form.noticeUserList.push({
            personName: u.userName,
            directoryName: u.directoryName,
            directoryId: u.directoryId,
            personId: u.id
          })
        }
      })
    },
    alarmLevelFormat(row) {
      return this.selectDictLabel(this.alarmLevel, row.alarmLevel)
    },
    planTypeFormat(row) {
      return this.selectDictLabel(this.planTypes, row.planType)
    },
    /** 查询预警发布管理列表 */
    getList() {
      this.loading = true
      listRelease(this.queryParams).then(response => {
        this.releaseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    confAlarmChange(val) {
      const item = this.plansNameAll.find(e => {
        return e.id == val
      })
      if (item) {
        this.form.planType = item.planType
        this.form.alarmLevel = item.alarmLevel
        this.form.planAbstract = item.planAbstract
        this.form.planTitle = item.planTitle
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        alarmLevel: null,
        planType: null,
        planTitle: null,
        planAbstract: null,
        alarmTime: null,
        noticeUserList: [],
        noticeType: null,
        createUser: null,
        createTime: null,
        status: 0
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加预警发布管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getRelease(id).then(response => {
        this.form = response.data
        this.form.noticeUserList = response.data.noticeUserList || []
        // this.form.noticeType
        this.formDisabled = false
        this.open = true
        this.title = '修改预警发布管理'
      })
    },
    handleView(row) {
      this.reset()
      const id = row.id || this.ids
      getRelease(id).then(response => {
        this.form = response.data
        this.form.noticeUserList = response.data.noticeUserList || []
        // this.form.noticeType
        this.formDisabled = true
        this.open = true
        this.title = '修改预警发布管理'
      })
    },
    handleRelease(row) {
      this.reset()
      const id = row.id || this.ids
      const _this = this
      getRelease(id).then(response => {
        const form = response.data
        this.$confirm('是否确认发布?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          form.status = 1
          updateRelease(form).then(response => {
            _this.msgSuccess('操作成功')
            _this.open = false
            _this.getList()
          })
        }).catch(() => {})
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRelease(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addRelease(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除预警发布管理编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delRelease(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lc/release/export', {
        ...this.queryParams
      }, `lc_release.xlsx`)
    }
  }
}
</script>
<style>
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
</style>
